home *** CD-ROM | disk | FTP | other *** search
- Path: garden.csc.calpoly.edu!not-for-mail
- From: dstubbs@garden.csc.calpoly.edu (Dan Stubbs)
- Newsgroups: comp.lang.c
- Subject: Re: while loop problem
- Date: 16 Mar 1996 09:27:02 -0800
- Organization: Cal Poly, San Luis Obispo
- Message-ID: <4ietl6$pqm@garden.csc.calpoly.edu>
- References: <Pine.OSF.3.91.960312133449.7844B-100000@io.UWinnipeg.ca> <Do6DB0.EtE@microunity.com> <Pine.A32.3.91.960312171258.149477C-100000@black.weeg.uiowa.edu> <DoBAC8.K3o@iquest.net>
- NNTP-Posting-User: dstubbs@garden.csc.calpoly.edu
-
- In article <DoBAC8.K3o@iquest.net>, Doug Miller <dlmiller@iquest.net> wrote:
- >The Amorphous Mass <robinson@blue.weeg.uiowa.edu> wrote:
- >
- >+On Tue, 12 Mar 1996, Tom Sanders wrote:
- >+
- >+> Bill Simpson <wsimpson@uwinnipeg.ca> writes:
- >+> |> Consider the following code fragment:
- >+> |>
- >+> |> y=2000; z=1000;
- >+> |> x=0;
- >+> |> while(x<=XMAX)
- >+> |> {
- >+> |> x+=(int) erand(mean);
- >+> |> setdot(x,y,z);
- >+> |> }
- >+> |>
- >+> |> The above code will also attempt to plot one point at an x value >XMAX
- >+> |> before it terminates.
- >+> |>
- >+> |> Is there a nice way to write the code so it works properly?
- >+>
- >
- >Well, let's give him a correct answer.
- >
- >In the *original* loop, just change the test (x <= XMAX) to (x < XMAX).
- >There. That wasn't so hard, was it?
- >
-
- No, it wasn't so hard, and it also is not correct. The problem is
- updating the value of x and then writing it before testing to make
- sure the new x value is acceptable. Several posters have given correct
- solutions. Here is one of them again.
-
- y=2000; z=1000;
- x+=(int) erand(mean);
- while (x <= XMAX)
- {
- setdot(x,y,z);
- x+=(int) erand(mean);
- }
-
-